from typing import Optional


class ExamQuestionDetail:
    """考试题目详情实体类"""
    def __init__(self,
                 id: Optional[int] = None,
                 exam_id: Optional[int] = None,
                 question_id: Optional[int] = None,
                 question_text: Optional[str] = None,
                 option_a: Optional[str] = None,
                 option_b: Optional[str] = None,
                 option_c: Optional[str] = None,
                 option_d: Optional[str] = None,
                 correct_answer: Optional[str] = None,
                 user_answer: Optional[str] = None,
                 is_correct: Optional[bool] = None,
                 type: Optional[int] = None,
                 image_data: Optional[str] = None):
        """
        初始化考试题目详情实体
        :param id: 详情ID
        :param exam_id: 考试ID
        :param question_id: 题目ID
        :param question_text: 题目文本
        :param option_a: 选项A
        :param option_b: 选项B
        :param option_c: 选项C
        :param option_d: 选项D
        :param correct_answer: 正确答案
        :param user_answer: 用户答案
        :param is_correct: 是否正确
        :param type: 题目类型
        :param image_data: 图片数据(可选)
        """
        self.id = id
        self.exam_id = exam_id
        self.question_id = question_id
        self.question_text = question_text
        self.option_a = option_a
        self.option_b = option_b
        self.option_c = option_c
        self.option_d = option_d
        self.correct_answer = correct_answer
        self.user_answer = user_answer
        self.is_correct = is_correct
        self.type = type
        self.image_data = image_data

    def __dict__(self) -> dict:
        """将实体转换为字典"""
        return {
            'id': self.id,
            'exam_id': self.exam_id,
            'question_id': self.question_id,
            'question_text': self.question_text,
            'option_a': self.option_a,
            'option_b': self.option_b,
            'option_c': self.option_c,
            'option_d': self.option_d,
            'correct_answer': self.correct_answer,
            'user_answer': self.user_answer,
            'is_correct': self.is_correct,
            'type': self.type,
            'image_data': self.image_data
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'ExamQuestionDetail':
        """从字典创建实体"""
        return cls(
            id=data.get('id'),
            exam_id=data.get('exam_id'),
            question_id=data.get('question_id'),
            question_text=data.get('question_text'),
            option_a=data.get('option_a'),
            option_b=data.get('option_b'),
            option_c=data.get('option_c'),
            option_d=data.get('option_d'),
            correct_answer=data.get('correct_answer'),
            user_answer=data.get('user_answer'),
            is_correct=data.get('is_correct'),
            type=data.get('type'),
            image_data=data.get('image_data')
        )